bondscell_results$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8queued¤logsrunning¦outputbody.

Object-Oriented Behaviour

Object-oriented languages tightly couple the class (or composite type) and the method (or function). This is achieved via single dispatch, which means that only the first argument is used to detemine which function to call or execute. Python is a good example of a single dispatch language.

Julia loosely couples composite types (classes) and functions (methods) because of multiple dispatach, which means that all positional arguments, or type signature, are used to determine which function to call or execute. Therefore, Julia is not an object-oriented language.

mimetext/htmlrootassigneelast_run_timestampA}Q~Ѱpersist_js_state÷has_pluto_hook_features§cell_id$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$70c42e39-490a-4fc2-acfc-c4c0553a4e1equeued¤logsrunning¦outputbody٩

1-6: Functions and Types

Last Updated: 2 May 2026

mimetext/htmlrootassigneelast_run_timestampA}Q~#1persist_js_state·has_pluto_hook_features§cell_id$70c42e39-490a-4fc2-acfc-c4c0553a4e1edepends_on_disabled_cells§runtime9ypublished_object_keysdepends_on_skipped_cells§errored$5c195e5e-3929-4448-9b6e-f3b989f2e297queued¤logsrunning¦outputbody#

mimetext/htmlrootassigneelast_run_timestampA}Q~rpersist_js_state÷has_pluto_hook_features§cell_id$5c195e5e-3929-4448-9b6e-f3b989f2e297depends_on_disabled_cells§runtime- published_object_keysdepends_on_skipped_cells§errored$dbf2d122-5751-447e-9a6b-6997fe9a07fcqueued¤logsrunning¦outputbody

Constructor Functions

Functions can be constructors for composite types. Assume a composite type MyType has two fields a and b:

struct MyType
    a::Integer
    b::Integer
end

Julia automatically creates a default constructor function:

function MyType(a::Integer, b::Integer)
    new(a, b)
end

with usage of:

MyType(1, 3)

Now assume the second argument b is often 0, then we can define a function of the same name with the second argument having a default value of 0.

function MyType(a::Integer, b::Integer=0)
    MyType(a, b)
end

with usage of:

MyType(1)
mimetext/htmlrootassigneelast_run_timestampA}Q~Jpersist_js_state÷has_pluto_hook_features§cell_id$dbf2d122-5751-447e-9a6b-6997fe9a07fcdepends_on_disabled_cells§runtimeCpublished_object_keysdepends_on_skipped_cells§errored$2d156a39-9fa7-4c16-9fcb-beccfd318c03queued¤logsrunning¦outputbody

1: A function with one abstract type

mimetext/htmlrootassigneelast_run_timestampA}Q~;persist_js_state÷has_pluto_hook_features§cell_id$2d156a39-9fa7-4c16-9fcb-beccfd318c03depends_on_disabled_cells§runtimeUkpublished_object_keysdepends_on_skipped_cells§errored$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0dequeued¤logsrunning¦outputbody

Let Julia Do It

Let's assume that we have to write a function myfunc to do some data analysis. The function has one argument a. It has the same implementation for all floating point data types.

AbstractFloat tells Julia to only allow floating point types for the argument. Integers or Strings are not allowed. A new version or method of the function is compiled for each new data type. Thus, the user only needs to write one version of the function. Julia will create new versions of the function for each new argument type when needed.

This feature makes Julia a very productive language by reducing the number of lines of code that need to be written. Julia is typically twice as productive as Python and ten times as productive as C/C++ with similar or better performance.

Note

Productivity is inversely proportional to the number of lines of code. Fewer lines of code result in greater productivity.

mimetext/htmlrootassigneelast_run_timestampA}Q~Cpersist_js_state÷has_pluto_hook_features§cell_id$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0dedepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$092154d4-5072-4855-bba6-69500c2dcf31queued¤logsrunning¦outputbody

4: Object-oriented behaviour

mimetext/htmlrootassigneelast_run_timestampA}Q~ persist_js_state÷has_pluto_hook_features§cell_id$092154d4-5072-4855-bba6-69500c2dcf31depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$5588e7ba-1f51-441b-bdc2-fe63023e42e6queued¤logsrunning¦outputbody

3: A constructor function

mimetext/htmlrootassigneelast_run_timestampA}Q~5ΰpersist_js_state÷has_pluto_hook_features§cell_id$5588e7ba-1f51-441b-bdc2-fe63023e42e6depends_on_disabled_cells§runtimegpublished_object_keysdepends_on_skipped_cells§errored$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751queued¤logsrunning¦outputbody<

Summary

mimetext/htmlrootassigneelast_run_timestampA}Q~persist_js_state÷has_pluto_hook_features§cell_id$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751depends_on_disabled_cells§runtime ~mpublished_object_keysdepends_on_skipped_cells§errored$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dqueued¤logsrunning¦outputbodyP mimetext/htmlrootassigneelast_run_timestampA}Q~"gpersist_js_state·has_pluto_hook_features§cell_id$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992ddepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$666f9c05-4801-4be5-8fa0-7eb8a882517bqueued¤logsrunning¦outputbody

Problems

Remember that you can get help either through `?` in a REPL or with "Live Docs" right here in Pluto (lower right-hand corner)

mimetext/htmlrootassigneelast_run_timestampA}Q~-{persist_js_state÷has_pluto_hook_features§cell_id$666f9c05-4801-4be5-8fa0-7eb8a882517bdepends_on_disabled_cells§runtime3published_object_keysdepends_on_skipped_cells§errored$36360e9c-a3fa-463d-b3ed-fef4f4851007queued¤logsrunning¦outputbody

5: Functors

mimetext/htmlrootassigneelast_run_timestampA}Q~Űpersist_js_state÷has_pluto_hook_features§cell_id$36360e9c-a3fa-463d-b3ed-fef4f4851007depends_on_disabled_cells§runtime TXpublished_object_keysdepends_on_skipped_cells§errored$c43d1c95-e003-4580-9057-ac59970f17c2queued¤logsrunning¦outputbody

2: A function with two abstract types

mimetext/htmlrootassigneelast_run_timestampA}Q~"persist_js_state÷has_pluto_hook_features§cell_id$c43d1c95-e003-4580-9057-ac59970f17c2depends_on_disabled_cells§runtimejpublished_object_keysdepends_on_skipped_cells§errored$74314d5e-64d6-46a1-88ee-cd5fc84972e3queued¤logsrunning¦outputbody

Functors

Functors are nameless functions. They are defined by their argument type, usually a composite type. A good example of this behaviour is the polynomial.

mimetext/htmlrootassigneelast_run_timestampA}Q~persist_js_state÷has_pluto_hook_features§cell_id$74314d5e-64d6-46a1-88ee-cd5fc84972e3depends_on_disabled_cells§runtimeEpublished_object_keysdepends_on_skipped_cells§errored±cell_dependencies$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8precedence_heuristic cell_id$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8downstream_cells_mapupstream_cells_map@md_strgetindex$70c42e39-490a-4fc2-acfc-c4c0553a4e1eprecedence_heuristic cell_id$70c42e39-490a-4fc2-acfc-c4c0553a4e1edownstream_cells_mapupstream_cells_maptodayDates$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dDates.format|>@dateformat_strMarkdown$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d$5c195e5e-3929-4448-9b6e-f3b989f2e297precedence_heuristic cell_id$5c195e5e-3929-4448-9b6e-f3b989f2e297downstream_cells_mapupstream_cells_map@md_strgetindex$dbf2d122-5751-447e-9a6b-6997fe9a07fcprecedence_heuristic cell_id$dbf2d122-5751-447e-9a6b-6997fe9a07fcdownstream_cells_mapupstream_cells_map@md_strgetindex$2d156a39-9fa7-4c16-9fcb-beccfd318c03precedence_heuristic cell_id$2d156a39-9fa7-4c16-9fcb-beccfd318c03downstream_cells_mapupstream_cells_map@md_strgetindex$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0deprecedence_heuristic cell_id$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0dedownstream_cells_mapupstream_cells_map@md_strgetindex$092154d4-5072-4855-bba6-69500c2dcf31precedence_heuristic cell_id$092154d4-5072-4855-bba6-69500c2dcf31downstream_cells_mapupstream_cells_map@md_strgetindex$5588e7ba-1f51-441b-bdc2-fe63023e42e6precedence_heuristic cell_id$5588e7ba-1f51-441b-bdc2-fe63023e42e6downstream_cells_mapupstream_cells_map@md_strgetindex$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751precedence_heuristic cell_id$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751downstream_cells_mapupstream_cells_map@md_strgetindex$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dprecedence_heuristiccell_id$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992ddownstream_cells_mapDates$70c42e39-490a-4fc2-acfc-c4c0553a4e1eInteractiveUtilsPlutoUIMarkdown$70c42e39-490a-4fc2-acfc-c4c0553a4e1eupstream_cells_mapTableOfContents$666f9c05-4801-4be5-8fa0-7eb8a882517bprecedence_heuristic cell_id$666f9c05-4801-4be5-8fa0-7eb8a882517bdownstream_cells_mapupstream_cells_map@md_strgetindex$36360e9c-a3fa-463d-b3ed-fef4f4851007precedence_heuristic cell_id$36360e9c-a3fa-463d-b3ed-fef4f4851007downstream_cells_mapupstream_cells_map@md_strgetindex$c43d1c95-e003-4580-9057-ac59970f17c2precedence_heuristic cell_id$c43d1c95-e003-4580-9057-ac59970f17c2downstream_cells_mapupstream_cells_map@md_strgetindex$74314d5e-64d6-46a1-88ee-cd5fc84972e3precedence_heuristic cell_id$74314d5e-64d6-46a1-88ee-cd5fc84972e3downstream_cells_mapupstream_cells_map@md_strgetindexcell_execution_order$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d$70c42e39-490a-4fc2-acfc-c4c0553a4e1e$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de$dbf2d122-5751-447e-9a6b-6997fe9a07fc$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8$74314d5e-64d6-46a1-88ee-cd5fc84972e3$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751$666f9c05-4801-4be5-8fa0-7eb8a882517b$2d156a39-9fa7-4c16-9fcb-beccfd318c03$c43d1c95-e003-4580-9057-ac59970f17c2$5588e7ba-1f51-441b-bdc2-fe63023e42e6$092154d4-5072-4855-bba6-69500c2dcf31$36360e9c-a3fa-463d-b3ed-fef4f4851007$5c195e5e-3929-4448-9b6e-f3b989f2e297last_hot_reload_timeshortpath1-6: Function-Types.jlprocess_statusreadypathD/home/runner/work/workshop/workshop/notebooks/1-6: Function-Types.jlpluto_versionv0.20.24last_save_timeA}Q|,cell_order$70c42e39-490a-4fc2-acfc-c4c0553a4e1e$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de$dbf2d122-5751-447e-9a6b-6997fe9a07fc$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8$74314d5e-64d6-46a1-88ee-cd5fc84972e3$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751$666f9c05-4801-4be5-8fa0-7eb8a882517b$2d156a39-9fa7-4c16-9fcb-beccfd318c03$c43d1c95-e003-4580-9057-ac59970f17c2$5588e7ba-1f51-441b-bdc2-fe63023e42e6$092154d4-5072-4855-bba6-69500c2dcf31$36360e9c-a3fa-463d-b3ed-fef4f4851007$5c195e5e-3929-4448-9b6e-f3b989f2e297$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dpublished_objectsnbpkginstall_time_ns`ʬinstantiatedòinstalled_versionsDatesstdlibInteractiveUtilsstdlibPlutoUI0.7.77Markdownstdlibterminal_outputsDatesI Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Project.toml`  Updating `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Manifest.toml` [14a3606d] ↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4 Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...nbpkg_syncI Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Project.toml`  Updating `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Manifest.toml` [14a3606d] ↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4 Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...InteractiveUtilsI Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Project.toml`  Updating `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Manifest.toml` [14a3606d] ↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4 Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...PlutoUII Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Project.toml`  Updating `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Manifest.toml` [14a3606d] ↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4 Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...MarkdownI Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Project.toml`  Updating `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_fqfgkmadqp/Manifest.toml` [14a3606d] ↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4 Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...enabled÷restart_recommended_msgrestart_required_msgbusy_packageswaiting_for_permission,waiting_for_permission_but_probably_disabled«cell_inputs$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8cell_id$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8codemd""" # Object-Oriented Behaviour Object-oriented languages tightly couple the class (or composite type) and the method (or function). This is achieved via **single dispatch**, which means that only the first argument is used to detemine which function to call or execute. Python is a good example of a single dispatch language. * For Python, the method may look like the following, where `self` is the class (composite type): ```python def mymethod(self, a, b): ``` The method is usually invoked using the dot notation: ```python self.mymethod(a, b) ``` Julia loosely couples composite types (classes) and functions (methods) because of **multiple dispatach**, which means that all positional arguments, or type signature, are used to determine which function to call or execute. Therefore, Julia is not an object-oriented language. * For Julia, the function may look like the following, where `mytype` is the composite type (class): ```julia mymethod(mytype, a, b) ``` """metadatashow_logsèdisabled®skip_as_script«code_folded$70c42e39-490a-4fc2-acfc-c4c0553a4e1ecell_id$70c42e39-490a-4fc2-acfc-c4c0553a4e1ecode""" !!! note "1-6: Functions and Types" **Last Updated: $(Dates.format(today(), dateformat"d u Y"))** """ |> Markdown.parsemetadatashow_logsèdisabled®skip_as_script«code_folded$5c195e5e-3929-4448-9b6e-f3b989f2e297cell_id$5c195e5e-3929-4448-9b6e-f3b989f2e297codemd""" ---- """metadatashow_logsèdisabled®skip_as_script«code_folded$dbf2d122-5751-447e-9a6b-6997fe9a07fccell_id$dbf2d122-5751-447e-9a6b-6997fe9a07fccodemd""" # Constructor Functions Functions can be constructors for composite types. Assume a composite type `MyType` has two fields `a` and `b`: ```julia struct MyType a::Integer b::Integer end ``` Julia automatically creates a default constructor function: ```julia function MyType(a::Integer, b::Integer) new(a, b) end ``` with usage of: ```julia MyType(1, 3) ``` Now assume the second argument `b` is often `0`, then we can define a function of the same name with the second argument having a default value of `0`. ```julia function MyType(a::Integer, b::Integer=0) MyType(a, b) end ``` with usage of: ```julia MyType(1) ``` """metadatashow_logsèdisabled®skip_as_script«code_folded$2d156a39-9fa7-4c16-9fcb-beccfd318c03cell_id$2d156a39-9fa7-4c16-9fcb-beccfd318c03code2md""" ## 1: A function with one abstract type !!! warning "" * Create a function having one argument being an abstract float type. * Use the function with a `Float64` (double precision) type, e.g., `1.0` or `1.0e0`. * Use the function with a `Float32` (single precision) type, e.g., `1.0f0`. * Note: the `f` means Float32, whereas `e` means Float64. * How many methods does it have? * Hint: use the `methods()` function, e.g., `methods(myfunc)`. * Use the function with a `BigFloat` type. * How many methods are there now? """metadatashow_logsèdisabled®skip_as_script«code_folded$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0decell_id$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0decode~md""" # Let Julia Do It Let's assume that we have to write a function `myfunc` to do some data analysis. The function has one argument `a`. It has the same implementation for all floating point data types. * For statically compiled languages such as C, each argument type must have its own version of the function, i.e., `myfunc(a::Float32)`, `myfunc(a::Float64)`, etc. * For interpreted languages such as Python, only one version is needed, but performance suffers because the interpreter must determine the type of the argument. * For Julia, only one version of the function is needed, assuming the argument type is an `AbstractFloat`, i.e., `myfunc(a::AbstractFloat)` `AbstractFloat` tells Julia to only allow floating point types for the argument. `Integer`s or `String`s are not allowed. A new version or method of the function is compiled for each new data type. Thus, the user only needs to write **one version** of the function. Julia will create new versions of the function for each new argument type when needed. This feature makes Julia a very productive language by reducing the number of lines of code that need to be written. Julia is typically twice as productive as Python and ten times as productive as C/C++ with similar or better performance. !!! note Productivity is inversely proportional to the number of lines of code. Fewer lines of code result in greater productivity. """metadatashow_logsèdisabled®skip_as_script«code_folded$092154d4-5072-4855-bba6-69500c2dcf31cell_id$092154d4-5072-4855-bba6-69500c2dcf31code"md""" ## 4: Object-oriented behaviour !!! warning "" * Overload the add operator by executing `import Base.+` . * Create a 2D point type. * Hint: `struct Point{R} x::R, y::R end`. * Create a variable using the Point type. * Create add function for the Point type. * Hint: `function +(a::Point, b::Point)`. * How many methods does the `Base.+` function have? * Add the two points together, e.g., `Point(1, 2) + Point(3, 4)`. * Create a norm function for the Point type. * Evaluate the norm for a point. """metadatashow_logsèdisabled®skip_as_script«code_folded$5588e7ba-1f51-441b-bdc2-fe63023e42e6cell_id$5588e7ba-1f51-441b-bdc2-fe63023e42e6codemd""" ## 3: A constructor function !!! warning "" * Create a composite type with two fields. * Instantiate the type, e.g., `a = MyType(1, 2)`. * Create a function of the same name having a default second argument. * Use the function with one and two arguments. """metadatashow_logsèdisabled®skip_as_script«code_folded$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751cell_id$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751codemd""" # Summary !!! note "" * Abstract types allow you to write functions for a specific set of types. * Julia creates a new version of the function based on the argument types. * Functions can be used to simplify composite type constructors. * Julia is not an object-oriented language, but is behaves like one. * Functors are nameless functions that use the composite type for dispatch. """metadatashow_logsèdisabled®skip_as_script«code_folded$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dcell_id$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dcode}begin using Markdown, Dates, PlutoUI, InteractiveUtils TableOfContents(; title = "1-6: Functions and Types", depth = 4) endmetadatashow_logsèdisabled®skip_as_script«code_folded$666f9c05-4801-4be5-8fa0-7eb8a882517bcell_id$666f9c05-4801-4be5-8fa0-7eb8a882517bcodeٜmd""" # Problems !!! tip "Remember that you can get help either through `?` in a REPL or with "Live Docs" right here in Pluto (lower right-hand corner)" """metadatashow_logsèdisabled®skip_as_script«code_folded$36360e9c-a3fa-463d-b3ed-fef4f4851007cell_id$36360e9c-a3fa-463d-b3ed-fef4f4851007code md""" ## 5: Functors !!! warning "" * Create a polynomial type. * Create a function to evaluate the polynomial at a value `x`. * Hint: use the `sum()` function and an array comprehension. * Create the polynomial. * Evaluate the polynomial. """metadatashow_logsèdisabled®skip_as_script«code_folded$c43d1c95-e003-4580-9057-ac59970f17c2cell_id$c43d1c95-e003-4580-9057-ac59970f17c2code-md""" ## 2: A function with two abstract types !!! warning "" * Create a `add` function having two arguments, the first being an abstract float and the second an abstract integer. * Use the function with float and integer arguments. * Use the function with integer and float arguments. """metadatashow_logsèdisabled®skip_as_script«code_folded$74314d5e-64d6-46a1-88ee-cd5fc84972e3cell_id$74314d5e-64d6-46a1-88ee-cd5fc84972e3codemd""" # Functors Functors are nameless functions. They are defined by their argument type, usually a composite type. A good example of this behaviour is the polynomial. * Construct the polynomial type: ```julia struct Polynomial{R} coef::Vector{R} end ``` * Construct the function to evaluate the polynomial: ```julia function (p::Polynomial)(x) ... end ``` * Create the polynomial: ```julia p = Polynomial([1, 2, 3]) ``` * Evaluate the polynomial ```julia p(3) ``` """metadatashow_logsèdisabled®skip_as_script«code_foldedënotebook_id$4baf7be0-45be-11f1-8eca-dfbda6fd96a6in_temp_dir¨metadata